package edu.hubu.book;

import edu.hubu.book.domain.BookClass;
import edu.hubu.book.service.IBookClassService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import java.util.List;

/**
 * @author moonlan
 * @date 2020/4/29 13:00
 */
@SuppressWarnings(value = "unused")
@SpringBootTest
public class EnvBook {

    @Autowired private IBookClassService testService;

    /**
     * 测试spring  / spring整合Mybatis成功
     */
//    @Test
//    public void testFindAll() {
//        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:config/springConfig/springContext.xml");
//        System.out.println("容器创建成功");
//        IBookClassService bs = ac.getBean("bookClassService", IBookClassService.class);
//        List<BookClass> books = bs.findAll();
//        for (BookClass b : books) {
//            System.out.println(b);
//        }
//    }

    //    /**
    //     * 单独测试Mybatis
    //     */
    //    @Test
    //    public void test() {
    //        List<BookClass> books = testDao.findAll();
    //        for (BookClass b : books) {
    //            System.out.println(b);
    //        }
    //
    //    }

    /**
     * 测试添加一级分类
     */
    @Test
    public void testAddFirstClass() {
        BookClass bookClass = new BookClass();
        bookClass.setFirstClassName("选修课程");
        bookClass.setDescription("大学选修");
        int flag = testService.addFirstClass(bookClass);
        if (flag == 0) {
            System.out.println("添加一级分类失败!");
        } else {
            System.out.println("添加一级分类成功！");
        }
    }

    /**
     * 测试添加二级分类
     */
    @Test
    public void testAddSecondClass() {
        BookClass bookClass = new BookClass();
        bookClass.setFirstId(5);
        bookClass.setSecondClassName("Hadoop大数据技术");
        int flag = testService.addSecondClass(bookClass);
        if (flag == 0) {
            System.out.println("添加二级分类失败!");
        } else {
            System.out.println("添加二级分类成功！");
        }
    }

    /**
     * 测试修改一级分类
     */
    @Test
    public void testUpdateFirstClass() {
        BookClass bookClass = new BookClass();
        bookClass.setSid(5);
        bookClass.setFirstClassName("自然科学");
        bookClass.setDescription("了解自然");
        int flag = testService.updateFirstClass(bookClass);
        if (flag == 0) {
            System.out.println("修改一级分类失败!");
        } else {
            System.out.println("修改一级分类成功！");
        }
    }

    /**
     * 测试修改二级分类
     */
    @Test
    public void testUpdateSecondClass() {
        BookClass bookClass = new BookClass();
        bookClass.setSsid(6);
        bookClass.setFirstId(5);
        bookClass.setSecondClassName("大数据导论");
        int flag = testService.updateSecondClass(bookClass);
        if (flag == 0) {
            System.out.println("修改失败!");
        } else {
            System.out.println("修改成功！");
        }
    }

    /**
     * 测试删除二级分类
     */
    @Test
    public void testDeleteSecondClass() {
        BookClass bookClass = new BookClass();
        bookClass.setSecondClassName("三体");        //先执行上面修改二级分类
        int flag = testService.deleteSecondClass(bookClass);
        if (flag == 0) {
            System.out.println("删除失败!");
        } else {
            System.out.println("删除成功！");
        }
    }

    /**
     * 测试删除一级分类（对应二级分类全部删除）
     */
    @Test
    public void testDeleteFirstClass() {
        BookClass bookClass = new BookClass();
        bookClass.setFirstClassName("自然科学");        //根据名称删除
        int flag = testService.deleteFirstClass(bookClass);
        if (flag == 0) {
            System.out.println("删除失败!");
        } else {
            System.out.println("删除成功！");
        }
    }


}
